<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Surat_keluar extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('mdl_reff_unit');
		$this->load->model('mdl_t_disposisi');
		$this->load->model('mdl_t_approval');
		$this->load->model('mdl_t_surat_keluar');
		$this->load->model('mdl_t_admin');
	}

	public function index() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}else{
			$this->surat_keluar();
		}

		/*$a['page']	= "d_amain";

		$this->load->view('admin/aaa', $a);*/


	}

	public function surat_keluar() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}



		/* pagination */
		$total_row		= $this->db->query("SELECT * FROM t_surat_keluar")->num_rows();
		$per_page		= 10;

		$awal	= $this->uri->segment(4);
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;

		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;

		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."surat_keluar/p");

		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);

		$cari					= addslashes($this->input->post('q'));

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));


		$kode					= addslashes($this->input->post('kode'));
		$dari					= addslashes($this->input->post('dari'));
		$no_surat				= addslashes($this->input->post('no_surat'));
		$tgl_surat				= addslashes($this->input->post('tgl_surat'));
		$uraian					= addslashes($this->input->post('uraian'));
		$ket					= addslashes($this->input->post('ket'));
		$kirim_ke					= $this->input->post('kirim_ke');

		$cari					= addslashes($this->input->post('q'));
		$no_agenda				= '';
		if ($this->session->userdata('admin_level')=='TU'){
			$no_agenda				= addslashes($this->input->post('no_agenda'));
		}

		//upload config
		$config['upload_path'] 		= './upload/surat_keluar';
		$config['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
		$config['max_size']			= '2000';
		$config['max_width']  		= '3000';
		$config['max_height'] 		= '3000';

		$this->load->library('upload', $config);



		$level_user=$this->session->userdata('admin_level');
		$kd_unit=$this->session->userdata('kd_unit');
		$var_escape_self_id = substr($kd_unit, -2, 2);


		$level_user=$this->session->userdata('admin_level');
		$a['level_user'] = $level_user;


		if ($mau_ke == "del") {
			$this->db->query("DELETE FROM t_surat_keluar WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('surat_keluar');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM t_surat_keluar WHERE isi_ringkas LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "surat_keluar/l_surat_keluar";
		} else if ($mau_ke == "add") {
			$a['page']		= "surat_keluar/f_surat_keluar";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM t_surat_keluar WHERE gen_log_surat = '$idu'")->row();
			$a['page']		= "surat_keluar/f_surat_keluar";
		} else if ($mau_ke == "act_add") {

			//$data_app['id_unit'] = 'app' . date('Yhismd');
		/*	if ($this->upload->do_upload('file_surat')) {
				$up_data	 	= $this->upload->data();

				$this->db->query("INSERT INTO t_surat_keluar VALUES (NULL, '$kode', '$no_agenda', '$uraian', '$dari', '$no_surat', '$tgl_surat', NOW(), '$ket', '".$up_data['file_name']."', '".$this->session->userdata('admin_id')."')");
			} else {

			}*/
			/*for($j=0;$j<count($kpd_yth);$j++){
					$data_app['approval'] = $kpd_yth[$j];
					$this->mdl_t_approval->insert($data_app);
			}*/
			$gen_log_surat = $this->mdl_t_surat_keluar->get_gen_kode();
			$this->db->query("INSERT INTO t_surat_keluar VALUES (NULL, '$kode', '$no_agenda', '$uraian',
				'$dari','" . $kirim_ke . "', '$no_surat', '$tgl_surat', NOW(), '$ket', '',
				 '".$this->session->userdata('admin_id')."', '" . $gen_log_surat . "')");
			$data['id_surat']='';
			$data['userid']=$this->session->userdata('admin_id');
			$data['isi_surat']=$uraian;
			$data['kirim_ke']=$kirim_ke;
			$data['date']=date('Y-m-d');
			$data['time']=date('H:i:s');
			$data['time']=date('H:i:s');
			$data['gen_log_surat']=$gen_log_surat;

			$this->mdl_t_approval->insert_log($data);

			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
			redirect('surat_keluar');
		} else if ($mau_ke == "act_edt") {
		/*	if ($this->upload->do_upload('file_surat')) {
				$up_data	 	= $this->upload->data();

				$this->db->query("UPDATE t_surat_keluar SET no_agenda = '$no_agenda', kode = '$kode', isi_ringkas = '$uraian', tujuan = '$dari', no_surat = '$no_surat', tgl_surat = '$tgl_surat', keterangan = '$ket', file = '".$up_data['file_name']."' WHERE id = '$idp'");
			} else {
				$this->db->query("UPDATE t_surat_keluar SET no_agenda = '$no_agenda', kode = '$kode', isi_ringkas = '$uraian', tujuan = '$dari', no_surat = '$no_surat', tgl_surat = '$tgl_surat', keterangan = '$ket' WHERE id = '$idp'");
			}
*/
			$this->db->query("UPDATE t_surat_keluar SET no_agenda = '$no_agenda', kode = '$kode', isi_ringkas = \"" . $uraian . "\", tujuan = '$dari', kirim_ke = '" . $kirim_ke . "',  no_surat = '$no_surat', tgl_surat = '$tgl_surat', keterangan = '$ket' WHERE gen_log_surat = '$idp'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated ".$this->upload->display_errors()."</div>");
			redirect('surat_keluar');
		} else {
			// $arr_where = ' where ori_kirim_ke="' . $this->session->userdata('admin_id') . '" or log_kirim_ke="' . $this->session->userdata('admin_id') . '"';
			$userid= $this->session->userdata('admin_id');
			$filter['kirim_ke'] = $userid;
			$filter['userid'] = $userid;

			$res_t_sk_n_log = $this->mdl_t_surat_keluar->get_surat_keluar_n_log($akhir,$awal, $filter);
			$a['data'] = $res_t_sk_n_log->result();


			$a['page']		= "surat_keluar/l_surat_keluar";
		}

		$this->load->view('admin/aaa', $a);
	}

	function perbaiki(){
		$idu=$this->uri->segment(3);
		$filter['id'] = $idu;
		$a['datpil']= $this->mdl_t_surat_keluar->get_surat_keluar_n_log_by_id(0,0, $filter)->row();
		//$a['datpil']	= $this->db->query("SELECT * FROM t_surat_keluar WHERE gen_log_surat = '$idu'")->row();
		$a['page']		= "surat_keluar/f_perbaiki_surat";

		$this->load->view('admin/aaa', $a);
	}

	function proses_perbaiki(){
		$id_surat=$this->input->post('idp');
		$data['id_surat'] = $id_surat;
		$data['userid']=$this->session->userdata('admin_id');
		$data['isi_surat']=$this->input->post('uraian');
		$gen_log_surat=$this->input->post('gen_log_surat');
		$data['gen_log_surat']=$gen_log_surat;
		$data['date']=date('Y-m-d');
		$data['time']=date('H:i:s');
		$data['kirim_ke']='0';

		$datas['id']=$this->input->post('id_log');
		$datas['id_surat']=$id_surat;

		$this->mdl_t_approval->insert_log($data);
		$this->mdl_t_approval->update_revision($datas);

		if ($this->session->userdata('admin_level')=='TU'){

			$data_up['id'] = $id_surat;
			$data_up['no_agenda']=$this->input->post('no_agenda');
			$this->mdl_t_surat_keluar->update($data_up);
		}




		redirect('surat_keluar');
	}

	function show_kirim(){
		$idu=$this->input->post('idsurat');
		$filter['id'] = $idu;
		//$a['datpil']	= $this->db->query("SELECT * FROM t_surat_keluar WHERE gen_log_surat = '$idu'")->row();
		$a['datpil']= $this->mdl_t_surat_keluar->get_surat_keluar_n_log_by_id(0,0, $filter)->row();
		$this->load->view('admin/surat_keluar/show_kirim', $a, FALSE);
	}

	function proses_kirim(){
		$data['id']=$this->input->post('idp');
		$data['userid']=$this->session->userdata('admin_id');
		$data['kirim_ke']=$this->input->post('kirim_ke');

		$this->mdl_t_approval->update_log($data);
	}

	function show_history(){

		$filter['gen_log_surat'] = $this->input->post('gen_log_surat');
		$a['data']= $this->mdl_t_surat_keluar->get_surat_keluar_n_log_by_id_surat(0,0, $filter);
		$this->load->view('admin/surat_keluar/show_history', $a, FALSE);
	}

	function show_detail(){
		$idu=$this->input->post('idlog');
		$filter['id'] = $idu;
		$a['results']= $this->mdl_t_surat_keluar->get_detail_id(0,0, $filter);
		$this->load->view('admin/surat_keluar/show_detail', $a, FALSE);
	}

	function cetak(){
		$idu = $this->uri->segment(3);
		$filter['id'] = $idu;
		$a['results']= $this->mdl_t_surat_keluar->get_detail_id(0,0, $filter)->row();
		$this->load->view('admin/surat_keluar/cetak', $a);
	}

}